const baseUrl = '';

function fun(url, method, data, headerType) {
  data = data || {};
  let header = {};
  let token = wx.getStorageSync("token");
  console.log(token)
  if (token) {
    if (!header || !header["token"]) {
      header["token"] = token;
    }
  }
  if (headerType){
    header["Content-Type"] = 'application/json';
  }else{
    header["Content-Type"] = 'application/x-www-form-urlencoded';
  }
  wx.showNavigationBarLoading();
  let promise = new Promise(function (resolve, reject) {
    wx.request({
      url: baseUrl + url,
      header: header,
      data: data,
      method: method,
      success: function (res) {
        if (typeof res.data === "object") {
          if (res.data.code) {
            if (res.data.code === '50008') {
              wx.showToast({
                title: "登录超时，请重新登录",
                icon: "none"
              });
              setTimeout(function () {
                wx.switchTab({
                  url: "/pages/index/index"
                });
              }, 3000);
              reject("请重新登录");
            } else if (res.data.code === 403) {
              wx.showToast({
                title: '没有权限,'+res.data.msg,
                icon: "none"
              });
              setTimeout(function () {
                wx.navigateTo({
                  url: "/pages/login/index"
                });
              }, 3000);
              reject(res.data.msg);
            } else if (res.data.code === 401) {
              wx.showToast({
                title: '没有登录，请重新登录!,' + res.data.msg,
                icon: "none"
              });
              setTimeout(function () {
                wx.navigateTo({
                  url: "/pages/login/index"
                });
              }, 3000);
              reject(res.data.msg);
            } else if (res.data.code === 407) {
              wx.showToast({
                title: '登录超时，请重新登录!,' + res.data.msg,
                icon: "none"
              });
              setTimeout(function () {
                wx.navigateTo({
                  url: "/pages/login/index"
                });
              }, 3000);
              reject(res.data.msg);
            }
          }
        }
        resolve(res);
      },
      fail: reject,
      complete: function (e) {
        wx.hideNavigationBarLoading();
      }
    });

  });
  return promise;
}
function upload(url, name, filePath,formData) {
  let header = {};
  let token = wx.getStorageSync("token"); //从缓存中拿该信息
  if (token) {
    if (!header || !header["token"]) {
      header["token"] = token; //添加到请求头中
    }
  }
  //header["Content-Type"] = 'multipart/form-data';
  wx.showNavigationBarLoading();
  let promise = new Promise(function (resolve, reject) {
    wx.uploadFile({
      url: apiHttp + url,
      filePath: filePath,
      name: name,
      formData: formData,
      header: header,
      success: function (res) {
        if (typeof res.data != "object") {
          let rsData = JSON.parse(res.data);
          if (rsData.code) {
            if (rsData.code === '50008') {
              wx.showToast({
                title: "登录超时，请重新登录",
                icon: "none"
              });
              setTimeout(function () {
                wx.switchTab({
                  url: "/pages/index/index"
                });
              }, 1000);
              reject("请重新登录");
            } else if (rsData.code === '403') {
              wx.showToast({
                title: '没有权限,' + rsData.message,
                icon: "none"
              });
              setTimeout(function () {
                wx.switchTab({
                  url: "/pages/index/index"
                });
              }, 1000);
              reject(rsData.msg);
            }
          }
        }
        resolve(res);
      },
      fail: reject,
      complete: function () {
        wx.hideNavigationBarLoading();
      }
    });
  });
  return promise;
}
module.exports = {
  baseUrl: baseUrl,
  "get": function (url, data, header) {
    return fun(url, "GET", data, header);
  },
  "post": function (url, data, header) {
    return fun(url, "POST", data, header);
  },
  upload: function (url, name, filePath, formData) {
    return upload(url, name, filePath, formData);
  }
};